
/**
 * 
 */
package com.pmsco.pms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.pmsco.pms.ajaxdata.MedicineJsonData;
import com.pmsco.pms.ajaxdata.MedicineListJsonData;
import com.pmsco.pms.annotation.LogonRequired;
import com.pmsco.pms.annotation.PermissionRequired;
import com.pmsco.pms.business.MedicineBusiness;
import com.pmsco.pms.business.dto.MedicineRequestDTO;
import com.pmsco.pms.business.dto.MedicineReturnDTO;
import com.pmsco.pms.core.constant.PmsConstant;
import com.pmsco.pms.core.domain.Medicine;
import com.pmsco.pms.util.PmsLogonRequired;
import com.pmsco.pms.util.PmsPermissionRequired;

/**
 * @author Tran Thanh Long
 *
 */
@Controller
public class PmsMedicineController extends AbstractPmsController{
	private static final Logger logger = LoggerFactory.getLogger(PmsMedicineController.class);
	
	/**
	 * List PMS Medicine
	 */
	@RequestMapping(value = PmsConstant.listMedicine_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_LIST_MEDICINE_FUNCTION)
	public String listMedicine(Locale locale, Model model) {
		logger.info("List Medicine Action");
		return PmsConstant.listMedicine_View;
	}
	
	/**
	 * List PMS Medicine Data
	 */
	@RequestMapping(value = PmsConstant.listMedicineData_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_LIST_MEDICINE_FUNCTION)
	public @ResponseBody MedicineListJsonData listMedicineData(HttpServletRequest request, Locale locale, Model model) {
		logger.info("Begin List Medicine Data Action");
		MedicineBusiness medicineBusiness = new MedicineBusiness();
		
		logger.debug("Build list medicine request dto");
		MedicineRequestDTO medicineRequestDTO = new MedicineRequestDTO();
		Integer page = Integer.parseInt(request.getParameter("page"));
		Integer rows = Integer.parseInt(request.getParameter("rows"));
		String sidx = request.getParameter("sidx");
		String sord = request.getParameter("sord");
		String searchField = request.getParameter("searchField");
		String searchString = request.getParameter("searchString");
		String searchOper = request.getParameter("searchOper");
		if(page == null)
			page = 1;
		if(rows == null)
			rows = 10;
		medicineRequestDTO.setPage(page);
		medicineRequestDTO.setRows(rows);
		medicineRequestDTO.setSidx(sidx);
		medicineRequestDTO.setSord(sord);
		medicineRequestDTO.setSearchField(searchField);
		medicineRequestDTO.setSearchString(searchString);
		medicineRequestDTO.setSearchOper(searchOper);
		
		logger.debug("Call to pms medicine business for list medicine");
		MedicineReturnDTO medicineReturnDTO = medicineBusiness.listMedicineData(medicineRequestDTO);
		
		logger.debug("Analysis list medicine return dto");
		MedicineListJsonData medicineListJsonData = new MedicineListJsonData();
		Integer records = medicineReturnDTO.getMedicinesSize();
		Integer total = (records / rows);
		if(records % rows > 0)
			total += 1;
		medicineListJsonData.setPage(page);
		medicineListJsonData.setRecords(records);
		medicineListJsonData.setTotal(total);
		List<MedicineJsonData> data = new ArrayList<MedicineJsonData>();
		for(Medicine medicine : medicineReturnDTO.getMedicines()) {
			data.add(new MedicineJsonData(medicine));
		}
		medicineListJsonData.setRows(data);
		
		logger.info("End List Medicine Data Action");
		return medicineListJsonData;
	}
	
	/**
	 * Edit PMS Medicine
	 */
	@RequestMapping(value = PmsConstant.editMedicine_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_EDIT_MEDICINE_FUNCTION)
	public ModelAndView editMedicine(@RequestParam(value = "id") Long id) {
		logger.info("Begin Edit Medicine Action");
		MedicineBusiness medicineBusiness = new MedicineBusiness();
		
		logger.debug("Build edit medicine request dto");
		MedicineRequestDTO medicineRequestDTO = new MedicineRequestDTO();
		medicineRequestDTO.setMedicineId(id);
		
		logger.debug("Call to medicine business for edit medicine");
		MedicineReturnDTO medicineReturnDTO = medicineBusiness.editMedicine(medicineRequestDTO);
		
		logger.debug("Analysis create medicine return dto");
		ModelAndView ret = new ModelAndView(medicineReturnDTO.getReturnView(), "command", medicineReturnDTO.getMedicine());
		ret.addObject(PmsConstant.categoryList, medicineReturnDTO.getCategoryList());
		
		logger.info("End Edit Medicine Action");
		return ret;
	}
	
	/**
	 * View PMS Medicine
	 */
	@RequestMapping(value = PmsConstant.viewMedicine_Url)
	public ModelAndView viewMedicine(@RequestParam(value = "id") Long id) {
		logger.info("Begin View Medicine Action");
		MedicineBusiness medicineBusiness = new MedicineBusiness();
		
		logger.debug("Build view medicine request dto");
		MedicineRequestDTO medicineRequestDTO = new MedicineRequestDTO();
		medicineRequestDTO.setMedicineId(id);
		
		logger.debug("Call to medicine business for view medicine");
		MedicineReturnDTO medicineReturnDTO = medicineBusiness.viewMedicine(medicineRequestDTO);
		
		logger.debug("Analysis create medicine return dto");
		ModelAndView ret = new ModelAndView(medicineReturnDTO.getReturnView(), "command", medicineReturnDTO.getMedicine());
		
		logger.info("End View Medicine Action");
		return ret;
	}
	
	/**
	 * Create PMS Medicine
	 */
	@RequestMapping(value = PmsConstant.createMedicine_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_CREATE_MEDICINE_FUNCTION)
	public ModelAndView createMedicine(Locale locale, Model model) {
		logger.info("Begin Create Medicine Action");
		MedicineBusiness medicineBusiness = new MedicineBusiness();
		
		logger.debug("Call to medicine business for create medicine");
		MedicineReturnDTO medicineReturnDTO = medicineBusiness.createMedicine(new MedicineRequestDTO());
		
		logger.debug("Analysis create medicine return dto");
		ModelAndView ret = new ModelAndView(medicineReturnDTO.getReturnView(), "command", medicineReturnDTO.getMedicine());
		ret.addObject(PmsConstant.categoryList, medicineReturnDTO.getCategoryList());
		
		logger.info("End Create Medicine Action");
		return ret;
	}
	
	/**
	 * Save PMS Medicine
	 */
	@RequestMapping(value = PmsConstant.saveMedicine_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_SAVE_MEDICINE_FUNCTION)
	public ModelAndView saveMedicine(@ModelAttribute("medicineDetail") Medicine medicineDetail, Locale locale, Model model, HttpServletRequest request) {
		logger.info("Begin Save Medicine Action");
		MedicineBusiness medicineBusiness = new MedicineBusiness();
		
		logger.debug("Build save medicine request dto");
		MedicineRequestDTO medicineRequestDTO = new MedicineRequestDTO();
		
		medicineRequestDTO.setDirtyMedicine(medicineDetail);
		String[] categories = request.getParameterValues("category");
		String[] subCategories = request.getParameterValues("subCategory");
		String[] mainSubCategories = request.getParameterValues("mainSubCategory");
		medicineRequestDTO.setCategories(categories);
		medicineRequestDTO.setSubCategories(subCategories);
		medicineRequestDTO.setMainSubCategories(mainSubCategories);
		
		logger.debug("Call to medicine business for save medicine");
		MedicineReturnDTO medicineReturnDTO = medicineBusiness.saveMedicine(medicineRequestDTO);
		
		logger.debug("Analysis save medicine return dto");
		ModelAndView ret = null;
		if(medicineReturnDTO.getReturnUrl() != null && !medicineReturnDTO.getReturnUrl().isEmpty()) {
			ret = new ModelAndView(new RedirectView(medicineReturnDTO.getReturnUrl()));
		} else {
			ret = new ModelAndView(medicineReturnDTO.getReturnView(), "command", medicineReturnDTO.getMedicine());
			ret.addObject(PmsConstant.errorMessage, medicineReturnDTO.getErrorMessage());
		}
		logger.info("End Save Medicine Action");
		return ret;
	}
	
	/**
	 * Delete PMS Medicine
	 */
	@RequestMapping(value = PmsConstant.deleteMedicine_Url)
	@LogonRequired(required = PmsLogonRequired.ADMIN_USER)
	@PermissionRequired(required = PmsPermissionRequired.PMS_DELETE_MEDICINE_FUNCTION)
	public ModelAndView deleteMedicine(Locale locale, Model model, HttpServletRequest request) {
		MedicineBusiness medicineBusiness = new MedicineBusiness();
		logger.info("Begin Delete Medicine Action");
		String selectedMedicineIds = request.getParameter("selectedMedicineIds");
		medicineBusiness.deleteMedicines(selectedMedicineIds);
		ModelAndView ret = new ModelAndView(new RedirectView(PmsConstant.listMedicine_View));
		logger.info("End Delete Medicine Action");
		return ret;
	}
}
